package com.houtai.service.train;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.houtai.base.ResultVo;
import com.houtai.bean.TdTrainApply;
import com.houtai.bean.dto.train.TdTrainDetailDto;
import com.houtai.bean.dto.train.TdTrainListDto;
import com.houtai.bean.dto.train.apply.TdTrainApplyInsertDto;
import com.houtai.bean.dto.train.apply.TdTrainApplySendMessageDto;
import com.houtai.bean.vo.train.TdTrainDetail;
import com.houtai.bean.vo.train.TdTrainDetailVo;
import com.houtai.bean.vo.train.TdTrainList;
import com.houtai.bean.vo.train.TdTrainListVo;
import com.houtai.common.util.CommonUtil;
import com.houtai.common.util.SendMessageUtil;
import com.houtai.dao.TdTrainApplyDao;
import com.houtai.dao.TdTrainDao;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Repository
public class TrainService {
    private static Logger logger = Logger.getLogger(TrainService.class);
    @Autowired
    private TdTrainDao tdTrainDao;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private TdTrainApplyDao tdTrainApplyDao;
    /**
     * @Purpose:    培训列表
     * @param:      dto
     * @Version:    1.0
     * @Author:     li
     * @time:       2019/8/21
     * @return:     TdTrainListVo
     */
    public TdTrainListVo trainList(TdTrainListDto dto) {
        String logInfo = this.getClass().getName() + ":trainList:";
        TdTrainListVo retInfo = new TdTrainListVo();
        logger.info("======" + logInfo + "begin======");
        try {
            String orderBy = "sort DESC,train_id desc";

            PageHelper.startPage(dto.getCurrent_page(), dto.getPage_size(), orderBy);
            List<TdTrainList> lists = tdTrainDao.findTdTrainList(dto);
            PageInfo<TdTrainList> list = new PageInfo<>(lists);
            if (list.getTotal() > 0) {

                retInfo.setCurrent_page(dto.getCurrent_page());
                retInfo.setList(list.getList());
                retInfo.setPage_size(list.getPageSize());
                retInfo.setTotal_record(list.getTotal());
                retInfo.setPages(list.getPages());
                retInfo.setMark("0");
                retInfo.setTip("查询成功");
            } else {
                retInfo.setMark("1");
                retInfo.setTip("无培训");
            }

        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    /**
     * @Purpose:    培训详情
     * @param:      dto
     * @Version:    1.0
     * @Author:     li
     * @time:       2019/8/21
     * @return:     ResultVo
     */
    public synchronized TdTrainDetailVo detail(TdTrainDetailDto dto) {
        String logInfo = this.getClass().getName() + ":detail:";
        TdTrainDetailVo retInfo = new TdTrainDetailVo();
        logger.info("======" + logInfo + "begin======");
        try {
            TdTrainDetail detail = tdTrainDao.findTdTrainDetail(dto.getTrain_id());
            if (detail != null) {
                retInfo.setDetail(detail);
                retInfo.setMark("0");
                retInfo.setTip("成功");
            } else {
                retInfo.setMark("1");
                retInfo.setTip("培训不存在");
            }

        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    /**
     * @Purpose:    报名培训发送短信
     * @param:      dto
     * @Version:    1.0
     * @Author:     li
     * @time:       2019/8/21
     * @return:     ResultVo
     */
    public synchronized ResultVo sendMessage(TdTrainApplySendMessageDto dto) {
        String logInfo =  ":sendMessage:";
        logger.info("======" + logInfo + "begin======");
        ResultVo vo = new ResultVo();
        try {
            TdTrainDetail detail = tdTrainDao.findTdTrainDetail(dto.getTrain_id());
            if (detail != null) {
                String code = CommonUtil.randomString(4);
                logger.info("验证码：" + code);
//                if (true) {
                if (SendMessageUtil.setRegisterSms(dto.getPhone_no(), code)) {
                    redisTemplate.opsForValue().set(dto.getPhone_no() + "trainapply", code, 5, TimeUnit.MINUTES);
                    vo.setMark("0");
                    vo.setTip("成功");
                } else {
                    vo.setMark("1");
                    vo.setTip("短信发送失败");
                }

            } else {
                vo.setMark("1");
                vo.setTip("培训不存在");
            }


        } catch (Exception e) {
            vo.setMark("-1");
            vo.setTip("json数据错误");
            logger.error("======error:" + e.toString() + "======");
        }
        logger.info("======" + logInfo + "end======");
        return vo;
    }
    /**
     * @Purpose:    报名培训
     * @param:      dto
     * @Version:    1.0
     * @Author:     li
     * @time:       2019/8/21
     * @return:     ResultVo
     */
    public synchronized ResultVo trainApply(TdTrainApplyInsertDto dto) {
        String logInfo =  ":trainApply:";
        logger.info("======" + logInfo + "begin======");
        ResultVo vo = new ResultVo();
        try {
            TdTrainDetail detail = tdTrainDao.findTdTrainDetail(dto.getTrain_id());
            if (detail != null) {
                if (redisTemplate.opsForValue().get(dto.getPhone_no() + "trainapply") != null) {
                    String code = (String) redisTemplate.opsForValue().get(dto.getPhone_no() + "trainapply");
                    if (code.equals(dto.getCode())) {
                        TdTrainApply tdTrainApply = tdTrainApplyDao.findTdTrainApply(dto.getPhone_no(), dto.getTrain_id());
                        if (tdTrainApply != null) {
                            vo.setMark("1");
                            vo.setTip("已报名该培训");
                        } else {
                            tdTrainApplyDao.insertTdTrainApply(dto);
                            vo.setMark("0");
                            vo.setTip("成功");

                        }

                    } else {
                        vo.setMark("1");
                        vo.setTip("验证码不正确");
                    }
                } else {
                    vo.setMark("1");
                    vo.setTip("验证码已失效");
                }


            } else {
                vo.setMark("1");
                vo.setTip("培训不存在");
            }


        } catch (Exception e) {
            vo.setMark("-1");
            vo.setTip("json数据错误");
            logger.error("======error:" + e.toString() + "======");
        }
        logger.info("======" + logInfo + "end======");
        return vo;
    }
}
